<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" label-width="110px">
      <el-row el-row type="flex" justify="space-between" :gutter="16">
        <el-col :span="18">
          <el-row >
            <el-col :span="6">
              <el-form-item label="车牌号/车架号:" prop="carNoVinNo">
                <el-input v-model="queryParams.carNoVinNo" clearable placeholder="请输入车牌号/车架号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="车辆归属城市:" prop="seriesName">
                <el-input v-model="queryParams.seriesName" clearable placeholder="请输入车系名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="品牌:" prop="brandName">
                <el-input v-model="queryParams.brandName" clearable placeholder="请输入车型名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="车系:" prop="seriesName">
                <el-input v-model="queryParams.seriesName" clearable placeholder="请输入车系名称"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row v-show="showSearch" >
            <el-col :span="6">
              <el-form-item label="车型:" prop="modelName">
                <el-input v-model="queryParams.modelName" clearable placeholder="请输入车型名称"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="车辆状态:" prop="carStatus">
                <el-select v-model="queryParams.carStatus" placeholder="请选择车辆状态">
                  <el-option v-for="item in carStatusList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="维保厂名称:" prop="maintFactory">
                <el-input v-model="queryParams.maintFactory" clearable placeholder="请输入维保厂名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="维保状态:" prop="maintStatus">
                <el-select v-model="queryParams.maintStatus" placeholder="请选择维保状态">
                  <el-option v-for="item in maintStatusList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row v-show="showSearch">
            <el-col :span="6">
              <el-form-item label="客户名称:" prop="customerName">
                <el-input v-model="queryParams.customerName" clearable placeholder="请输入客户名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="车辆所在城市:" prop="cityName">
                <el-input v-model="queryParams.cityName" clearable placeholder="请输入车型"></el-input>
              </el-form-item>
            </el-col>


          </el-row>
        </el-col>
        <el-col :span="6" style="display: flex;justify-content: space-between;">
          <div>
            <i class="el-icon-finished" style="color:#409EFF; font-size:16px;"></i>
            <el-button type="text" @click="() => showSearch = !showSearch"> <span
                style="font-size:14px;padding:0px 4px;">高级筛选</span></el-button>
            <i :class="!showSearch ? 'el-icon-arrow-down' : ' el-icon-arrow-up'"
              style="color:#409EFF; font-size:16px;"></i>
          </div>
          <div>
            <el-button type="primary" icon="el-icon-search" size="mini" @click.stop="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </div>


        </el-col>

      </el-row>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['demo:demo:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" border>
      <el-table-column label="操作" align="center" fixed="left" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="viewOpen(scope.row)" v-hasPermi="['demo:demo:remove']">查看</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)" 
            v-hasPermi="['demo:demo:edit']">删除</el-button>
        </template>
      </el-table-column>
      <template v-for=" i in tableCol">

        <el-table-column :label="i.label" align="center" v-if="i.key == 'businessType'" :width="i.width">
          <template slot-scope="{row}">
          
            <span v-show="row[i.key] == '1'">整备中</span>
           
            <span v-show="row[i.key] == '2'">整备中</span>
            <span v-show="row[i.key] == '3'">预定中</span>
           
           <span v-show="row[i.key] == '4'">用车中</span>
           <span v-show="row[i.key] == '5'">已过户</span>
           
           <span v-show="row[i.key] == '6'">已处置</span>
           <span v-show="row[i.key] == '7'">已出售</span>
          </template>
        </el-table-column>
        <el-table-column :label="i.label" align="center" v-else-if="i.key == 'maintenanceStatus'" :width="i.width">
          <template slot-scope="{row}">
            <span v-show="row[i.key] == 1" style="color:#70B603;">维修中</span>
            <span v-show="row[i.key] == 2">已完成</span>
            <span v-show="row[i.key] == 3" style="color:#FF0000;">已超时</span>
          </template>
        </el-table-column>
        <el-table-column :label="i.label" align="center" v-else-if="i.key == 'maintenance'" :width="i.width">
          <template slot-scope="{row}">
            <span v-show="row[i.key] == '1'" >报废</span>
            <span v-show="row[i.key] == '2'">轻微</span>
            <span v-show="row[i.key] == '3'" >出险</span>
          
          </template>
        </el-table-column>
        <el-table-column :label="i.label" align="center" :prop="i.prop" v-else :width="i.width"></el-table-column>
      </template>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>
  
<script>
import { getMaintenance, getMaintenanceDetails, delMaintenance } from "@/api/wxgl/maintenanceApi.js";
import { getToken } from "@/utils/auth";


export default {
  name: "MaintenanceList",
  components: {
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      carStatusList: [
        {
          value: '1', label: '正常'
        },

        {
          value: '2', label: '维修'
        },
        {
          value: '3', label: '报废'
        },


      ],
      maintStatusList: [

        {
          value: 1, label: '维修中'
        },
        {
          value: 2, label: '已完成'
        },
        {
          value: 3, label: '已超时'
        },

      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNoVinNo:"",
        brandName: "",
        seriesName: "",
        modelName: "",
        carStatus:"",
        maintFactory: "",
        maintStatus: "",
        customerName: "",
        cityName:"",
      },

      total: 0,
      dataList: [
        {
          carNo: "陕AUQWE12",
          vinNo: "124648",
          brandName: "宝马3",
          seriesName: "宝马",
          modelName: "宝马2",
          businessType: "2", 
          maintenanceStatus: "1",
          maintenance: "3", 
          customerName: "王其",
          expectStartTime: "2022-3-9 18:20:00", 
          expectEndTime: "2022-8-9 18:20:00",
          actualStartTime: "2022-3-9 18:20:00", 
          actualEndTime: "2022-8-9 18:20:00",
          city: "西安",
          cityName: "西安",
        }
      ],
      tableCol: [
        {
          prop: "carNo",
          label: "车牌号",
          width:"100"
        },
        {
          prop: "vinNo",
          label: "车架号",
          width:"100"
        },
        {
          prop: "brandName",
          label: "品牌",
          width:"100"

        },
        {
          prop: "seriesName",
          label: "车系",
          width:"100"

        },
        {
          prop: "modelName",
          label: "车型",
          width:"100"

        },
        {

          label: "车辆状态",
          key: "businessType",
          width:"100"

        },
        {

          label: "维修状态",
          key: "maintenanceStatus",
          width:"100"

        },
        {

          label: "维保类型",
          key: "maintenance",
          width:"100"

        },
        {
          prop: "customerName",
          label: "客户名称",
          width:"100"

        },
        {
          prop: "expectStartTime",
          label: "预计开始时间",
          width:"160",

        }
        , {
          prop: "expectEndTime",
          label: "预计结束时间",
          width:"160"

        },
        {
          prop: "actualStartTime",
          label: " 实际开始时间",
          width:"160"

        }
        , {
          prop: "actualEndTime",
          label: "实际结束时间",
          width:"160"

        },
        {
          prop:"city",
          label: "车辆归属城市",
          width:"100",

        }, {
          prop: "cityName",
          label: "所在城市",
          width:"100",

        }


      ],
      getBrandNameList: [],
      getSeriesNameList: [],
      getModelNameList: [],
      seriesGroupList: [],

      // 表单参数
      form: {
        brandId: "",
        brandName: "",
        seriesName: "",
        seriesId: "",
        modelId: "",
        modelName: "",
        shortName: "",
        seriesGroupName: "",
        seriesGroupId: "",
        price: "",
        year: "",
        makerId: "",
        makerName: "",

        // 车辆信息
        liter: "",
        dischargeStandard: "",
        fuelTypeId: "",
        fuelTypeName: "",
        enginePower: "",
        enginePowerKw: "",
        isGreen: "",
        modelCode: "",
        driveName: "",
        driveType: "",
        modelStatus: "",
        gearType: "",
        gearName: "",
        greenTypeId: "",
        greenTypeName: "",
        makerDate: "",
        minRegYear: "",
        maxRegYear: "",
        stopMakeYear: "",
        intake: "",
        seatNumber: "",
        bodyType: "",
        doorNumber: "",
        carStruct: "",
        isParallel: "",
        priceAllowance: "",
      },
      // 表单校验
      rules: {
        testKey: [
          { required: true, message: "key键不能为空", trigger: "blur" }
        ],
        value: [
          { required: true, message: "值不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // this.getList();
    // this.getBrandNames()
  },
  methods: {
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;


      getMaintenance(this.queryParams).then(res => {
        if (res.code == "200") {
          const { rows, total } = res
          this.dataList = rows
          this.total = total
          this.loading = false;
        }



      });
    },
    changeFuelType(val) {
      if (val) {
        if (this.fuelTypeList.length > 0) {
          let arr = this.fuelTypeList.filter(v => {
            return v.value == val
          })
          this.form.fuelTypeName = arr[0].label

        }
      } else {
        this.form.fuelTypeName = ""
      }
    },
    changeGreenType(val) {
      if (val) {
        if (this.greenTypeList.length > 0) {
          let arr = this.greenTypeList.filter(v => {
            return v.value == val
          })
          this.form.greenTypeName = arr[0].label

        }
      } else {
        this.form.greenTypeName = ""
      }
    },
    // 获取车系组
    getSeriesGroup(obj) {
      getSeriesGroupList(obj).then((res) => {
        this.seriesGroupList = res.map((v) => {
          if (typeof v.id === 'number') {
            v.id == `${v.id}`
          }
          return v
        })
      })
    },
    // 选择车系组
    changeSeriesGroup(val) {
      if (val) {
        if (this.seriesGroupList.length > 0) {
          let arr = this.seriesGroupList.filter(v => {
            return v.id == val
          })
          this.form.seriesGroupName = arr[0].seriesGroupName

        }
      } else {
        this.form.seriesGroupName = ""
      }
      this.$forceUpdate();
    },
    // 品牌名称组
    getBrandNames() {
      getBrandName({ brandName: "" }).then(res => {
        this.getBrandNameList = res || []
      })
    },
    changeBrand(id) {

      if (id) {
        this.seriesGroupList = []

        this.getSeriesNameList = []
        this.form.seriesId = ""
        this.form.seriesName = ""

        this.form.modelName = ""
        this.form.seriesGroupId = ""
        this.form.seriesGroupName = ""
        if (this.getBrandNameList.length > 0) {
          let arr = this.getBrandNameList.filter(v => {
            return v.id == id
          })
          this.form.brandName = arr[0].name
          this.getSeriesGroup({ brandName: this.form.brandName, carBrandId: id })
        }
        this.getSeriesNames(id)


      } else {
        this.form.seriesGroupId = ""
        this.form.seriesGroupName = ""
        this.form.brandName = ""
        this.form.seriesId = ""
        this.form.seriesName = ""

        this.form.modelName = ""

        this.getSeriesNameList = []
        this.seriesGroupList = []
      }

    },
    // 根据品牌id查询车系名称组
    getSeriesNames(id) {
      getSeriesName(id).then(res => {

        this.getSeriesNameList = res || []


      })
    },
    changeSeries(id) {
      if (id) {

        this.form.modelName = ""
        if (this.getSeriesNameList.length > 0) {
          let arr = this.getSeriesNameList.filter(v => {
            return v.id == id
          })
          this.form.seriesName = arr[0].name
        }
        // this.getModelNames(id)


      } else {

        this.form.seriesId = ""
        this.form.seriesName = ""

        this.form.modelName = ""
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        brandId: "",
        brandName: "",
        seriesName: "",
        seriesId: "",

        modelName: "",
        shortName: "",
        seriesGroupName: "",
        seriesGroupId: "",
        price: "",
        year: "",
        makerId: "",
        makerName: "",

        // 车辆信息
        liter: "",
        dischargeStandard: "",
        fuelTypeId: "",
        fuelTypeName: "",
        enginePower: "",
        enginePowerKw: "",
        isGreen: "",
        modelCode: "",
        driveName: "",
        driveType: "",
        modelStatus: "",
        gearType: "",
        gearName: "",
        greenTypeId: "",
        greenTypeName: "",
        makerDate: "",
        minRegYear: "",
        maxRegYear: "",
        stopMakeYear: "",
        intake: "",
        seatNumber: "",
        bodyType: "",
        doorNumber: "",
        carStruct: "",
        isParallel: "",
        priceAllowance: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {

      this.resetForm("queryForm");
      this.handleQuery();
    },

    viewOpen(row) {
      this.$router.push({ path: "/wxgl/maintenanceDetails", query: row })
      
    },
   
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id) {
            updateCar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            carBrandAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否删除？').then(() => {
        this.loading = true;
        return delMaintenance(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
  
    /** 导出按钮操作 */ 
    handleExport() { 
      this.download('car/carMaintenance/export', {
        ...this.queryParams
      }, `维修列表_${new Date().getTime()}.xlsx`)
    },
    
  }
};
</script>
